package com.he.hsdyc.service;

import com.he.hsdyc.common.ServiceException;
import com.he.hsdyc.dao.MemberDao;
import com.he.hsdyc.entity.Member;
import com.he.hsdyc.entity.vo.MemberVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

@Service
public class MemberServiceImpl implements MemberService {

    private final MemberDao memberDao;

    @Autowired
    public MemberServiceImpl(MemberDao memberDao) {
        this.memberDao = memberDao;
    }


    @Override
    public void insertUser(MemberVO memberVO) throws Exception {

        Member member = memberDao.findByMemberName(memberVO.getMemberName());
        if (member != null) {
            throw  new ServiceException("该用户已经注册过");
        }
        // 密码加密
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        memberVO.setPassword(bCryptPasswordEncoder.encode(memberVO.getPassword()));
        Date now = new Date();
        memberVO.setCreateTime(now);
        Member member1 = new Member();
        BeanUtils.copyProperties(memberVO, member1);
        memberDao.insert(member1);
    }

    @Override
    public Map<?, ?> login(String memberName, String password) {
        return null;
    }
}
